package com.zzyy.study.controller;

import com.zzyy.study.service.ChatAssistant;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.DocumentSplitter;
import dev.langchain4j.data.document.DocumentTransformer;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.parser.apache.tika.ApacheTikaDocumentParser;
import dev.langchain4j.data.document.splitter.DocumentByCharacterSplitter;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.data.segment.TextSegmentTransformer;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

/**
 * @auther zzyy
 * @create 2025-03-16 22:31
 */
@RestController
@Slf4j
public class RAGController
{
    @Resource
    InMemoryEmbeddingStore<TextSegment> embeddingStore;

    @Resource
    ChatAssistant chatAssistant;


    // http://localhost:9014/rag/add
    @GetMapping(value = "/rag/add")
    public String testAdd()
    {
        Document document = FileSystemDocumentLoader.loadDocument("D:\\44\\alibaba-java.docx");

        EmbeddingStoreIngestor.ingest(document, embeddingStore);

        String result = chatAssistant.chat("错误码00000和A0001分别是什么");

        System.out.println(result);

        return result;
    }

    //http://localhost:9014/rag/api
    @GetMapping(value = "/rag/api")
    public String ragApi() throws FileNotFoundException
    {
        FileInputStream fileInputStream = new FileInputStream("D:\\44\\alibaba-java.docx");
        Document document = new ApacheTikaDocumentParser().parse(fileInputStream);

        System.out.println(document);

        //docx文档转成txt
        return document.text();
    }






    //http://localhost:9014/rag/textSegment
    @GetMapping(value = "/rag/textSegment")
    public void ragTextSegment() throws FileNotFoundException
    {
        FileInputStream fileInputStream = new FileInputStream("D:\\44\\alibaba-java.docx");
        Document document = new ApacheTikaDocumentParser().parse(fileInputStream);

        List<TextSegment> split =
                new DocumentByCharacterSplitter(100, 0).split(document);

        System.out.println(split.size());

        System.out.println(split);

    }


}

